package com.unicom.priceboard.util;

import com.unicom.priceboard.data.vo.HaStock;
import com.unicom.priceboard.data.vo.HoStock;
import com.unicom.priceboard.data.vo.PutAdElement;
import com.unicom.priceboard.data.vo.PutExecElement;
import com.unicom.priceboard.thread.HaStockThread;
import com.unicom.priceboard.thread.PutAdThread;
import com.unicom.priceboard.thread.PutExecThread;
import com.unicom.priceboard.thread.SecurityThread;

public class HTML_Producer {
	public static int hastc_unit = 1000;
//	static String imgPath = Resources.getConfigMessage("priceboard.picture.prefix.path");
	static String imgPath = "";
	
	/**
	 * @param isChartDisplay
	 * @param strStocks
	 * @return HTML content for HASTC input stocks
	 */
	public static String hastcInitContent(int isChartDisplay,String strStocks) {
		StringBuffer buffer = new StringBuffer("");
		String[] arrSymbols = strStocks.equals("")?HaStockThread.getStockList().split(","):strStocks.split(",");
		int i=1;
		String css = "";
		for(String symbol : arrSymbols) {
			HaStock objStock = HaStockThread.getStock(symbol);
			String rowID = i%2==0?"row2":"row1";
			String matchingCSS = i%2==0?"row_22":"row_11";
			buffer.append("<tr id='"+rowID+"' name='"+symbol+"'>");
			String displayName = objStock.getStock_name().replaceAll("'", "").replaceAll("\"", "");
			
			//Symbol column
			buffer.append("<td style='cursor:pointer;' id='"+symbol+"symbol' title='"+displayName+"'>");
			buffer.append("<a class='symbolStock' onclick='portalLink(\""+symbol+"\")'>"+symbol+"</a>");
			buffer.append("</td>");
			
			//Chart
			if(isChartDisplay==1) {
				buffer.append("<td id='blackcol' align='center'>");
				if(objStock.getPriorClosePrice()!=0)
					buffer.append("<img src='"+imgPath+symbol+".png' class='iconchart'/>");
				else
					buffer.append("<img src='"+imgPath+"default.png' class='iconchart'/>");
				buffer.append("</td>");
			}
			
			//Reference price
			buffer.append("<td id='"+symbol+"priorClosePrice' align='right' class='Refprice'>");
			buffer.append(objStock.getMapValues().get("priorClosePrice"));
			buffer.append("</td>");
			
			//Ceiling price
			buffer.append("<td id='"+symbol+"ceiling' align='right' class='ceil'>");
			buffer.append(objStock.getMapValues().get("ceiling"));
			buffer.append("</td>");
			
			//Floor price
			buffer.append("<td id='"+symbol+"floor' align='right' class='floor'>");
			buffer.append(objStock.getMapValues().get("floor"));
			buffer.append("</td>");
			
			//Highest price
			css = objStock.getMapCss().get("highestPrice");
			buffer.append("<td id='"+symbol+"highestPrice' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("highestPrice"));
			buffer.append("</td>");
			
			//Lowest price
			css = objStock.getMapCss().get("lowestPrice");
			buffer.append("<td id='"+symbol+"lowestPrice' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("lowestPrice"));
			buffer.append("</td>");
			
			//TotalVol
			buffer.append("<td id='"+symbol+"totalVol' align='right' class='trade'>");
			buffer.append(objStock.getMapValues().get("totalVol"));
			buffer.append("</td>");
			
			//best3Bid
			css = objStock.getMapCss().get("best3Bid");
			buffer.append("<td id='"+symbol+"best3Bid' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best3Bid"));
			buffer.append("</td>");
			
			//best3BidVolume
			buffer.append("<td id='"+symbol+"best3BidVolume' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best3BidVolume"));
			buffer.append("</td>");
			
			//best2Bid
			css = objStock.getMapCss().get("best2Bid");
			buffer.append("<td id='"+symbol+"best2Bid' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best2Bid"));
			buffer.append("</td>");
			
			//best2BidVolume
			buffer.append("<td id='"+symbol+"best2BidVolume' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best2BidVolume"));
			buffer.append("</td>");
			
			//best1Bid
			css = objStock.getMapCss().get("best1Bid");
			buffer.append("<td id='"+symbol+"best1Bid' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best1Bid"));
			buffer.append("</td>");
			
			//best1BidVolume
			buffer.append("<td id='"+symbol+"best1BidVolume' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best1BidVolume"));
			buffer.append("</td>");
			
			//price
			css = objStock.getMapCss().get("price");
			buffer.append("<td align='right' class='"+matchingCSS+"'>");
			buffer.append("<span id='"+symbol+"price' class='"+css+"'>"+objStock.getMapValues().get("price")+"</span>");
			buffer.append("</td>");
			
			//vol
			buffer.append("<td align='right' class='"+matchingCSS+"'>");
			buffer.append("<span id='"+symbol+"vol' class='"+css+"'>"+objStock.getMapValues().get("vol")+"</span>");
			buffer.append("</td>");
			
			//Change
			buffer.append("<td align='right' class='"+matchingCSS+"'>");
			buffer.append("<span id='"+symbol+"change' class='"+css+"'>"+objStock.getMapValues().get("change")+"</span>");
			if(!objStock.getIndicator().equals(""))
				buffer.append("<img id='"+symbol+"indicator' src='"+objStock.getIndicator()+"' border='0'>");
			buffer.append("</td>");
			
			//best1Offer
			css = objStock.getMapCss().get("best1Offer");
			buffer.append("<td id='"+symbol+"best1Offer' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best1Offer"));
			buffer.append("</td>");
			
			//best1OfferVolume
			buffer.append("<td id='"+symbol+"best1OfferVolume' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best1OfferVolume"));
			buffer.append("</td>");
			
			//best2Offer
			css = objStock.getMapCss().get("best2Offer");
			buffer.append("<td id='"+symbol+"best2Offer' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best2Offer"));
			buffer.append("</td>");
			
			//best2OfferVolume
			buffer.append("<td id='"+symbol+"best2OfferVolume' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best2OfferVolume"));
			buffer.append("</td>");
			
			//best3Offer
			css = objStock.getMapCss().get("best3Offer");
			buffer.append("<td id='"+symbol+"best3Offer' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best3Offer"));
			buffer.append("</td>");
			
			//best3OfferVolume
			buffer.append("<td id='"+symbol+"best3OfferVolume' align='right' class='"+css+"'>");
			buffer.append(objStock.getMapValues().get("best3OfferVolume"));
			buffer.append("</td>");
			
			buffer.append("</tr>");
			i++;
		}
		
		return buffer.toString();
	}
	
	/**
	 * @param isChartDisplay
	 * @param strStocks
	 * @return HTML content for HOSE input stocks
	 */
	public static String hoseInitContent(int isChartDisplay,String strStocks) {
		String css = "";
		StringBuffer buffer = new StringBuffer("");
		String[] arrSymbols = strStocks.equals("")?SecurityThread.getStockList().split(","):strStocks.split(",");
		int i = 1;
		for (String symbol : arrSymbols) {
			HoStock objStock = SecurityThread.getStock(symbol);
			
			if(objStock!=null) {
				String rowID = i%2==0?"row2":"row1";
				String sessionCSS = i%2==0?"session_even":"session_odd";
				String matchingCSS = i%2==0?"row_22":"row_11";
				buffer.append("<tr id='"+rowID+"' name='"+symbol+"'>");
				
				//Symbol column
				buffer.append("<td style='cursor:pointer;' id='"+symbol+"symbol' title='"+objStock.getStock_name()+"'>");
				buffer.append("<a class='symbolStock' onclick='portalLink(\""+symbol+"\")' >"+symbol+"</a>");
				buffer.append("</td>");
				
				//Chart
				if(isChartDisplay==1) {
					buffer.append("<td id='blackcol' align='center'>");
					if(!objStock.getPriorClosePrice().equals("0"))
						buffer.append("<img src='"+imgPath+symbol+".png' class='iconchart'/>");
					else
						buffer.append("<img src='"+imgPath+"default.png' class='iconchart'/>");
					buffer.append("</td>");
				}
				
				//Reference price
				buffer.append("<td id='"+symbol+"priorClosePrice' align='right' class='Refprice'>");
				buffer.append(objStock.getMapValues().get("priorClosePrice"));
				buffer.append("</td>");
				
				//Ceiling price
				buffer.append("<td id='"+symbol+"ceiling' align='right' class='ceil'>");
				buffer.append(objStock.getCeiling());
				buffer.append("</td>");
				
				//Floor price
				buffer.append("<td id='"+symbol+"floor' align='right' class='floor'>");
				buffer.append(objStock.getMapValues().get("floor"));
				buffer.append("</td>");
				
				//highest price
				css = objStock.getMapCSS().get("highest_price");
				buffer.append("<td id='"+symbol+"highest_price' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("highest_price"));
				buffer.append("</td>");
				
				//lowest price
				css = objStock.getMapCSS().get("lowest_price");
				buffer.append("<td id='"+symbol+"lowest_price' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("lowest_price"));
				buffer.append("</td>");
				
				//FROOM
				buffer.append("<td id='"+symbol+"f_Room' align='right' class='trade'>");
				buffer.append(objStock.getMapValues().get("f_Room"));
				buffer.append("</td>");
				
				//priceOne
				css = objStock.getMapCSS().get("priceOne");
				buffer.append("<td align='right' class='"+sessionCSS+"'>");
				buffer.append("<span id='"+symbol+"priceOne' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("priceOne"));
				buffer.append("</span>");
				buffer.append("</td>");
				
				//volOne
				css = objStock.getMapCSS().get("volOne");
				buffer.append("<td align='right' class='"+sessionCSS+"'>");
				buffer.append("<span id='"+symbol+"volOne' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("volOne"));
				buffer.append("</span>");
				buffer.append("</td>");
				
				//priceTwo
				css = objStock.getMapCSS().get("priceTwo");
				buffer.append("<td align='right' class='"+sessionCSS+"'>");
				buffer.append("<span id='"+symbol+"priceTwo' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("priceTwo"));
				buffer.append("</span>");
				buffer.append("</td>");
				
				//volTwo
				css = objStock.getMapCSS().get("volTwo");
				buffer.append("<td align='right' class='"+sessionCSS+"'>");
				buffer.append("<span id='"+symbol+"volTwo' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("volTwo"));
				buffer.append("</span>");
				buffer.append("</td>");
				
				//best3Bid
				css = objStock.getMapCSS().get("best3Bid");
				buffer.append("<td id='"+symbol+"best3Bid' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best3Bid"));
				buffer.append("</td>");
				
				//best3BidVolume
				buffer.append("<td id='"+symbol+"best3BidVolume' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best3BidVolume"));
				buffer.append("</td>");
				
				//best2Bid
				css = objStock.getMapCSS().get("best2Bid");
				buffer.append("<td id='"+symbol+"best2Bid' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best2Bid"));
				buffer.append("</td>");
				
				//best2BidVolume
				buffer.append("<td id='"+symbol+"best2BidVolume' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best2BidVolume"));
				buffer.append("</td>");
				
				//best1Bid
				css = objStock.getMapCSS().get("best1Bid");
				buffer.append("<td id='"+symbol+"best1Bid' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best1Bid"));
				buffer.append("</td>");
				
				//best1BidVolume
				buffer.append("<td id='"+symbol+"best1BidVolume' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best1BidVolume"));
				buffer.append("</td>");
				
				//price
				css = objStock.getMapCSS().get("price");
				buffer.append("<td align='right' class='"+matchingCSS+"'>");
				buffer.append("<span id='"+symbol+"price' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("price"));
				buffer.append("</span>");
				buffer.append("</td>");
				
				//vol
				buffer.append("<td align='right' class='"+matchingCSS+"'>");
				buffer.append("<span id='"+symbol+"vol' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("vol"));
				buffer.append("</span>");
				buffer.append("</td>");
				
				//Change
				buffer.append("<td align='right' class='"+matchingCSS+"'>");
				buffer.append("<span id='"+symbol+"change' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("change"));
				buffer.append("</span>");
				if(!objStock.getIndicator().equals(""))
					buffer.append("<img id='"+symbol+"indicator' src='"+objStock.getIndicator()+"' border='0'>");
				buffer.append("</td>");
				
				//best1Offer
				css = objStock.getMapCSS().get("best1Offer");
				buffer.append("<td id='"+symbol+"best1Offer' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best1Offer"));
				buffer.append("</td>");
				
				//best1OfferVolume
				buffer.append("<td id='"+symbol+"best1OfferVolume' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best1OfferVolume"));
				buffer.append("</td>");
				
				//best2Offer
				css = objStock.getMapCSS().get("best2Offer");
				buffer.append("<td id='"+symbol+"best2Offer' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best2Offer"));
				buffer.append("</td>");
				
				//best2OfferVolume
				buffer.append("<td id='"+symbol+"best2OfferVolume' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best2OfferVolume"));
				buffer.append("</td>");
				
				//best3Offer
				css = objStock.getMapCSS().get("best3Offer");
				buffer.append("<td id='"+symbol+"best3Offer' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best3Offer"));
				buffer.append("</td>");
				
				//best3OfferVolume
				buffer.append("<td id='"+symbol+"best3OfferVolume' align='right' class='"+css+"'>");
				buffer.append(objStock.getMapValues().get("best3OfferVolume"));
				buffer.append("</td>");
				
				//tradingRoom
				buffer.append("<td id='"+symbol+"tradingRoom' align='right' class='trade'>");
				buffer.append(objStock.getMapValues().get("tradingRoom"));
				buffer.append("</td>");
				
				buffer.append("</tr>");
				i++;
			}
		}
		
		return buffer.toString();
	}
	
	/**
	 * @param type
	 * @return HTML content for PUTAD/PUTEXEC section
	 */
	public static String hoseInitPutThoughdeals(String type) {
		StringBuffer buffer = new StringBuffer("");
			String stockKey;
			boolean putAd=false;
			if(type.equals("Buy")){
				stockKey=PutAdThread.strBuy;
				putAd=true;
			}else if(type.equals("Sell")){
				stockKey=PutAdThread.strSell;
				putAd=true;
			}else{
				stockKey=PutExecThread.strConfirmNo;
			}
			
			String[] key=stockKey.split(",");
		
		int i = 1;
		if(putAd){
			for (String stock : key) {
				PutAdElement objElement=PutAdThread.mapPutAd.get(stock);
				if(objElement==null)
					continue;
				String symbolKey =objElement.getKey();
				buffer.append("<tr class='putThrough'>\n");
				buffer.append("<td align='center'");
				buffer.append("class='putThrough_Number'>\n");
				if(i<10){
					buffer.append("<span >0"+ i+"</span>\n");
				}else{
					buffer.append("<span >"+ i+"</span>\n");
				}
				buffer.append("</td>\n");
				
				if(type =="Buy" || type =="Sell"){
					buffer.append("<td id='"+symbolKey+"firmNo"+type+"'class='putThrough_No' align ='center'>\n");
					buffer.append(objElement.getFirmNo());
					buffer.append("</td>\n");
				}
				
				buffer.append("<td id='"+symbolKey+"Symbol"+type+"'class ='putThrough_symbol' align='center'>\n");
				buffer.append(objElement.getSymbol_code());
				buffer.append("</td>\n");
				
				buffer.append("<td id='"+symbolKey+"Price"+type+"'class ='putThrough_price' align='right'>\n");
				buffer.append(objElement.getPrice());
				buffer.append("</td>\n");
				
				buffer.append("<td id='"+symbolKey+"Volume"+type+"'class ='putThrough_volume' align='right'>\n");
				buffer.append(objElement.getVolume());
				buffer.append("</td>\n");
				
				buffer.append("</tr>\n");
				i++;
			}
		}else{
			for (String stock : key) {
				PutExecElement objElement=PutExecThread.mapPutExec.get(stock);
				if(objElement==null)
					continue;
				String symbol =objElement.getSymbolStock();
				buffer.append("<tr class='putThrough'>\n");
				buffer.append("<td align='center'");
				buffer.append("class='putThrough_Number'>\n");
				if(i<10){
					buffer.append("<span >0"+ i+"</span>\n");
				}else{
					buffer.append("<span >"+ i+"</span>\n");
				}
				buffer.append("</td>\n");
				
				buffer.append("<td id='"+symbol+"Symbol"+type+"'class ='putThrough_symbol' align='center'>\n");
				buffer.append(objElement.getSymbolStock());
				buffer.append("</td>\n");
				
				buffer.append("<td id='"+symbol+"Price"+type+"'class ='putThrough_price' align='right'>\n");
				buffer.append(objElement.getPrice());
				buffer.append("</td>\n");
				
				buffer.append("<td id='"+symbol+"Volume"+type+"'class ='putThrough_volume' align='right'>\n");
				buffer.append(objElement.getVolume());
				buffer.append("</td>\n");
				buffer.append("</tr>\n");
				i++;
			}
		}
		return buffer.toString();
	}
	
}
